package com.aacar.service.impl;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import com.aacar.constant.ConsFiled;
import com.aacar.constant.RedisConsField;
import com.aacar.dao.MemberDao;
import com.aacar.dto.RegisterDto;
import com.aacar.dto.UserDto;
import com.aacar.entity.Member;
import com.aacar.service.MemberService;
import com.aacar.service.TokenService;
import com.aacar.utils.ErrorCodeConsField;
import com.aacar.vo.CommonVo;
import com.aacar.vo.TokenVo;
import com.aacar.vo.UserInfoVo;
import com.aacar.vo.UserVo;

@Repository("memberService")
public class MemberServiceImpl implements MemberService {
	
	private Log logger = LogFactory.getLog(MemberServiceImpl.class);

	@Resource
	private MemberDao memberDao;
	
	@Autowired
	private TokenService tokenService;//获取token服务
	
	@Autowired  
    private RedisTemplate<String,String> redisTemplate;
	
	@Override
	public UserVo getMember(UserDto userDto) {
		Member member = new Member();
		member.setMobile(userDto.getPhoneNo());
		member.setPassWord(userDto.getPassWord());
		//1.获取用户基本信息
		UserVo uv = memberDao.getMember(member);
		if(null == uv) {
			return null;
		}
		//2.获取用户token信息
		TokenVo tv = tokenService.retrieveToken();
		String token = tv.getToken();
		logger.info("生成的token为:["+token+"]");
		uv.setToken(tv.getToken());
		//3.保存redis
		ValueOperations<String, String> valueOper = redisTemplate.opsForValue();  
        valueOper.set(RedisConsField.pre_login+token, token+uv.getMemberId());
		return uv;
	}

	@Override
	public CommonVo saveMember(RegisterDto registerDto) {
		Member member = new Member();
		member.setMobile(registerDto.getPhoneNo());//手机号
		member.setPassWord(registerDto.getPassword());//密码
		member.setIsFirstOrder(ConsFiled.YES);
//		member.setCreateTime(DateUtil.getNowDate()); //使用数据库当前时间
		member.setIsValid(ConsFiled.YES);
		member.setRankId("1");
		member.setRankName("普通用户");
		int result = memberDao.saveMember(member);
		CommonVo cv = new CommonVo();
		if (result > 0) {
			cv.setSuccess(true);
			cv.setInfo(ConsFiled.Register_Success);
		}else {
			cv.setSuccess(false);
			cv.setInfo(ConsFiled.Register_Error);
		}
		return cv;
	}

	@Override
	public int updateMember(Member member) {
		return memberDao.updateMember(member);
	}

	@Override
	public Member getMemberByUserId(long memberId) {
		Member member = new Member();
		member.setMemberId(memberId);
		return null;
//		return memberDao.getMember(member);
	}
	
	//通过手机号来查询是否已经注册过
	public CommonVo getCountByPhoneNo(String phoneNo) {
		Member member = new Member();
		member.setMobile(phoneNo);
		int count =  this.memberDao.getCountByPhoneNo(member);
		CommonVo cv = new CommonVo();
		if(count > 0) {
			cv.setSuccess(true);//不能使用
			cv.setInfo(ConsFiled.SUCCESS_INFO);
		}else {
			cv.setSuccess(false);
			cv.setInfo(ErrorCodeConsField.ERROR_MSG_10300);
		}
		return cv;
	}
	
	//获取个人信息
	public UserInfoVo getUserInfo(UserDto userDto) {
		Member member = new Member();
		member.setMemberId(userDto.getUserId());
		return this.memberDao.getUserInfo(member);
	}
	
	//修改个人信息
	public CommonVo updateUserInfo(UserDto userDto) {
		Member member = new Member();
//		member.setMemberId(userDto.getUserId());
		if(null != userDto.getConfirmPassword()) {
			member.setPassWord(userDto.getConfirmPassword());
		}else {
			member.setPassWord(userDto.getPassWord());
		}
		member.setMobile(userDto.getPhoneNo());
		member.setMemberId(userDto.getUserId());
		int result = this.memberDao.updateMember(member);
		CommonVo cv = new CommonVo();
		if(result > 0) {
			cv.setSuccess(true);
			cv.setInfo(ConsFiled.update_success);
		}else {
			cv.setSuccess(false);
			cv.setInfo(ConsFiled.update_error);
		}
		return cv;
	}


}
